home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / byt0187b.arc / LINKLIST.PAS next >
Pascal/Delphi Source File  |  1986-10-28  |  8KB  |  340 lines

  1. (**********************************************************************)
  2. (*                LINKLIST.PAS                  *)
  3. (*                                      *)
  4. (* This program maintains an ordered linked list of strings.  It is   *)
  5. (* designed for an Apple IIe or an Apple II+ with an 80-column card.  *)
  6. (* If you're using Pascal with 40 columns, all that has to be changed *)
  7. (* is the number 40 in the GOTOXY calls.  A printer is also assumed.  *)
  8. (* If you have none online, then one procedure call must be removed,  *)
  9. (* and it is marked as such in the program.                  *)
  10. (*                                      *)
  11. (* The program commands are as follows:                   *)
  12. (*    A)dd - adds string to list                      *)
  13. (*    D)elete - deletes string from list                  *)
  14. (*    B)lank - destroys list                          *)
  15. (*    P)rint - dumps list to printer                      *)
  16. (*    E)nd - terminates program                      *)
  17. (* Please note that on an Apple IIe the Caps Lock button must be      *)
  18. (* depressed for the program to accept these commands.              *)
  19. (**********************************************************************)
  20.  
  21. PROGRAM MANAGER;
  22.  
  23. { WARNING: shuts off range checking }
  24. (*$R-*)
  25.  
  26. TYPE
  27.     COMCHARS=SET OF CHAR;
  28.  
  29.     ST1=STRING[15];
  30.     ST2=STRING[6];
  31.  
  32.     LISTPOINT=^NODE;
  33.     NODE=RECORD
  34.         NAME:ST1;
  35.         LINK:LISTPOINT;
  36.          END;
  37.  
  38. VAR
  39.     LIST      : LISTPOINT;    { linked list head }
  40.     TARGET      : ST1;    { string to be manipulated }
  41.     COMMAND   : CHAR;    { input operation to list }
  42.     PROPCOMS  : COMCHARS;    { set of proper commands }
  43.     HEAP      : ^INTEGER;    { holds heap marker }
  44.  
  45. (*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*)
  46. (*                                   *)
  47. (*            PROCEDURES AND FUNCTIONS               *)
  48. (*                                   *)
  49. (*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*)
  50.  
  51.     (************************************************)
  52.     (*                        *)
  53.     (*             Previous            *)
  54.     (*                        *)
  55.     (************************************************)
  56.  
  57. { this procedure returns a pointer to the node before the target node or nil }
  58.  
  59. FUNCTION PREVIOUS(LIST:LISTPOINT; TARGET:ST1):LISTPOINT;
  60.  
  61. VAR
  62.     CURRENT:LISTPOINT; { current position pointer }
  63.  
  64. BEGIN
  65.     { CURRENT is initialized to beginning of list }
  66.     CURRENT:=LIST;
  67.  
  68.     { move node pointer until target or end of list is encountered }
  69.     WHILE (CURRENT^.LINK^.NAME<TARGET) AND (CURRENT^.LINK<>NIL)
  70.     DO BEGIN
  71.          CURRENT:=CURRENT^.LINK
  72.        END;
  73.  
  74.     PREVIOUS:=CURRENT
  75. END;
  76.  
  77.     (************************************************)
  78.     (*                        *)
  79.     (*              Add            *)
  80.     (*                        *)
  81.     (************************************************)
  82.  
  83.     { adds a node to the list in between two nodes }
  84.  
  85. PROCEDURE ADD(VAR PREV:LISTPOINT);
  86.  
  87. VAR
  88.     TEMP:LISTPOINT;
  89.  
  90. BEGIN
  91.     TEMP:=PREV^.LINK;
  92.     NEW(PREV^.LINK);
  93.     PREV^.LINK^.NAME:=TARGET;
  94.     PREV^.LINK^.LINK:=TEMP
  95. END;
  96.  
  97.     (************************************************)
  98.     (*                        *)
  99.     (*            Insert            *)
  100.     (*                        *)
  101.     (************************************************)
  102.  
  103.         { insert new node in list }
  104.  
  105. PROCEDURE INSERT(LIST:LISTPOINT;TARGET:ST1);
  106.  
  107. VAR
  108.     PREV:LISTPOINT;
  109.  
  110. BEGIN
  111.     PREV:=PREVIOUS(LIST,TARGET);
  112.  
  113.     { check to see if element is already in list }
  114.     IF PREV^.LINK^.NAME=TARGET
  115.       THEN
  116.         BEGIN
  117.         GOTOXY(40,10);
  118.         WRITELN('Element in list alread');
  119.         WRITE(CHR(7));WRITE(CHR(7))
  120.         END
  121.       ELSE
  122.         ADD(PREV)
  123. END;
  124.  
  125.     (************************************************)
  126.     (*                        *)
  127.     (*           Subtract            *)
  128.     (*                        *)
  129.     (************************************************)
  130.  
  131.          { remove a node in between two others }
  132.  
  133. PROCEDURE SUBTRACT(VAR PREV:LISTPOINT);
  134.  
  135. BEGIN
  136.     PREV^.LINK:=PREV^.LINK^.LINK
  137. END;
  138.  
  139.     (************************************************)
  140.     (*                        *)
  141.     (*            Delete            *)
  142.     (*                        *)
  143.     (************************************************)
  144.  
  145.         { delete a node from list }
  146.  
  147. PROCEDURE DELETE(LIST:LISTPOINT;TARGET:ST1);
  148.  
  149. VAR
  150.     PREV:LISTPOINT;
  151.  
  152. BEGIN
  153.     PREV:=PREVIOUS(LIST,TARGET);
  154.  
  155.     { check to see if target is in list }
  156.     IF (PREV^.LINK=NIL) OR (PREV^.LINK^.NAME<>TARGET)
  157.       THEN
  158.         BEGIN{ then }
  159.         GOTOXY(40,10);
  160.         WRITELN('Target not found !!!');
  161.         WRITE(CHR(7));WRITE(CHR(7))
  162.         END{ then }
  163.       ELSE
  164.         SUBTRACT(PREV)
  165. END;
  166.  
  167.     (************************************************)
  168.     (*                        *)
  169.     (*            Show Mem            *)
  170.     (*                        *)
  171.     (************************************************)
  172.  
  173.              { show memory left }
  174.  
  175. PROCEDURE SHOW_MEM;
  176.  
  177. BEGIN
  178.   { uses the built-in function MEMAVAIL to return number of works left }
  179.  
  180.     GOTOXY(40,20);
  181.  
  182.     { check to see if there is a reasonable amount of memory left }
  183.     IF MEMAVAIL>100
  184.       THEN
  185.         WRITE('There are ',MEMAVAIL:5,' words of memory left')
  186.       ELSE
  187.         BEGIN
  188.         WRITELN(':NEARING END OF MEMORY !!');
  189.         WRITE(CHR(7));WRITE(CHR(7))
  190.         END
  191. END;
  192.  
  193.     (************************************************)
  194.     (*                        *)
  195.     (*          Print List            *)
  196.     (*                        *)
  197.     (************************************************)
  198.  
  199.       { send contents of list to device specified }
  200.  
  201. PROCEDURE PRINT_LIST(LIST:LISTPOINT;DEVICE:ST1);
  202.  
  203. VAR
  204.     CURRENT:LISTPOINT;
  205.     OUT:TEXT; { variable representing output file }
  206. BEGIN
  207.  
  208.     { set up device communication }
  209.     REWRITE(OUT,DEVICE);
  210.     PAGE(OUT);
  211.  
  212.     CURRENT:=LIST;
  213.  
  214.     { send information to device }
  215.     WRITELN(OUT,'Current elements in the list are:');
  216.     WRITELN(OUT,'------- -------- -- --- ---- ---');
  217.  
  218.     WHILE CURRENT^.LINK<>NIL
  219.     DO BEGIN
  220.          WRITELN(OUT,CURRENT^.LINK^.NAME);
  221.          CURRENT:=CURRENT^.LINK
  222.        END;
  223.  
  224.     SHOW_MEM
  225. END; { print_list }
  226.  
  227.     (************************************************)
  228.     (*                        *)
  229.     (*            Get Name            *)
  230.     (*                        *)
  231.     (************************************************)
  232.  
  233.         { get string to be manipulated }
  234.  
  235. PROCEDURE GET_NAME(VAR TARGET:ST1;PROC:ST2);
  236.  
  237. BEGIN
  238.     GOTOXY(40,2);
  239.     WRITELN('Which string do you wish to ',PROC);
  240.     GOTOXY(40,3);
  241.     WRITE('-----> ');
  242.     READ(TARGET)
  243. END;
  244.  
  245.     (************************************************)
  246.     (*                        *)
  247.     (*            Kill List            *)
  248.     (*                        *)
  249.     (************************************************)
  250.  
  251.           { destroy contents of list }
  252.  
  253. PROCEDURE KILL_LIST(LIST:LISTPOINT);
  254.  
  255. BEGIN
  256.     RELEASE(HEAP);
  257.  
  258.     { tie up only link remaining after heap is destroyed }
  259.     LIST^.LINK:=NIL;
  260.  
  261.     PAGE(OUTPUT);
  262.     WRITELN('List is now empty.');
  263.     SHOW_MEM
  264. END;
  265.  
  266.     (************************************************)
  267.     (*                        *)
  268.     (*            Initialize            *)
  269.     (*                        *)
  270.     (************************************************)
  271.  
  272.             { create first node }
  273.  
  274. PROCEDURE INITIALIZE(VAR LIST:LISTPOINT);
  275.  
  276. BEGIN
  277.     { create list head }
  278.     NEW(LIST);
  279.     LIST^.LINK:=NIL;
  280.  
  281.     { set heap pointer }
  282.     MARK(HEAP)
  283. END;
  284.  
  285. (*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*)
  286. (*                                *)
  287. (*            MAIN PROGRAM                *)
  288. (*                                *)
  289. (*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*)
  290.  
  291. BEGIN{ main }
  292.     INITIALIZE(LIST);
  293.  
  294.     { set of proper inputs }
  295.     PROPCOMS:=['A','D','P','B','E'];
  296.  
  297.     WHILE COMMAND<>'E'
  298.     DO BEGIN{ while }
  299.  
  300.         GOTOXY(40,0);
  301.         WRITE('A(dd  B(lank  D(elete  P(rint  E(nd ');
  302.         READ(COMMAND);
  303.         IF (COMMAND IN PROPCOMS)
  304.           THEN
  305.           CASE COMMAND OF 
  306.             'A':BEGIN
  307.               GET_NAME(TARGET,'add');
  308.               INSERT(LIST,TARGET);
  309.               PRINT_LIST(LIST,'CONSOLE:')
  310.             END;
  311.  
  312.             'D':BEGIN
  313.               GET_NAME(TARGET,'delete');
  314.               DELETE(LIST,TARGET);
  315.               PRINT_LIST(LIST,'CONSOLE:')
  316.             END;
  317.  
  318.             { if you have no printer delete PRINT_LIST call
  319.               with PRINTER: as a parameter }
  320.  
  321.             'P':BEGIN
  322.               PRINT_LIST(LIST,'PRINTER:');
  323.               PRINT_LIST(LIST,'CONSOLE:')
  324.             END;
  325.  
  326.             'B':KILL_LIST(LIST)
  327.         END{ case }
  328.  
  329.     END{ while }
  330.  
  331. END.{ main }
  332.  
  333. ');
  334.               PRINT_LIST(LIST,'CONSOLE:')
  335.             END;
  336.  
  337.             'B':KILL_LIST(LIST)
  338.         END{ case }
  339.  
  340.     END{ w